package codingDudes.display.basic;

import codingDudes.display.DisplayObject;
import codingDudes.geom.Point2D;

/**
 * This class extends the DisplayObject class with a position history and
 * an advanced drawing method that is called for every history entry and provides the
 * current position and a ratio from 1 to 0 depending on the place in history.
 * 
 * Overwrite the draw(Point2D, float) method instead of the draw() method.
 * 
 * Use this class to draw position based effects like fadeout.
 * 
 * @author Tobias Friese
 */

public class FadingObject extends DisplayObject
{
	/* variables */
	private Point2D[] _positionHistory;
	
	// constructor
	/**
	 * @param steps The amount of positions cached in history.
	 */
	public FadingObject(int steps)
	{
		this._positionHistory = new Point2D[steps];
	}
	
	// methods
	private void shiftHistory(float x, float y)
	{
		for(int i = (this._positionHistory.length - 1); i > 0; i--)
		{
			this._positionHistory[i] = this._positionHistory[i - 1];
		}
		
		this._positionHistory[0] = new Point2D(x, y);
	}
	
	/**
	 * The drawing method calls the draw(Position2D, float) method for every entry
	 * in the history starting at the newest.
	 * 
	 * Don't extend this method. Extend draw(Position2D, float) for drawing.
	 */
	protected void draw()
	{
		int length = this._positionHistory.length;
		for(int i = (length - 1); i >= 0; i--)
		{
			Point2D currPos = this._positionHistory[i];
			if(currPos != null) this.draw(currPos, 1f - ((float)i / length));
		}
	}
	
	/**
	 * This method is called for every history entry.
	 * 
	 * Extend this method for drawing.
	 * 
	 * @param pos The current Position from history.
	 * @param ratio A floating number from 1 to 0 depending on the position in History.
	 */
	protected void draw(Point2D pos, float ratio)
	{
		
	}
	
	// overrides
	
	/**
	 * Shifts position history and renders as superclass.
	 */
	public void render()
	{
		this.shiftHistory(this.x, this.y);
		super.render();
	}
}
